package com.miao.admin.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.miao.admin.service.AdminOrderService;
import com.miao.admin.service.OperationService;
import com.miao.common.entity.R;
import com.miao.common.entity.pojo.Operation;
import com.miao.common.entity.pojo.ShopOrder;
import com.miao.common.utils.JWTUtil;
import com.miao.websecurity.entity.Admin;
import com.miao.websecurity.service.AdminService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单表(ShopOrder)表控制层
 *
 * @author MiaoWenQing
 * @since 2021-02-20 23:05:13
 */
@RestController
@RequestMapping("admin/shopOrder")
public class AdminOrderController {
    /**
     * 服务对象
     */
    @Resource
    private AdminOrderService adminOrderService;

    @Resource
    private AdminService adminService;

    @Resource
    private OperationService operationService;

    @Resource(name = "myRedisTemplate")
    RedisTemplate<String, Object> redisTemplate;


    /**
     * 获取店铺全部订单
     *
     * @param token
     * @return
     */
    @GetMapping("/orderList")
    public R orderList(@RequestHeader(value = "access-token") String token) {

        Admin admin = this.decodeToken(token);
        QueryWrapper<ShopOrder> shopOrderQueryWrapper = new QueryWrapper<>();
        shopOrderQueryWrapper.eq("store_id", admin.getStoreId());
        List<ShopOrder> list = this.adminOrderService.list(shopOrderQueryWrapper);
        return R.ok(list);
    }


    /**
     * 关闭订单
     *
     * @param oId   订单id
     * @param token token
     * @return R
     */

    @GetMapping("close")
    public R orderClose(String oId, String des, @RequestHeader(value = "access-token") String token) {
        Admin admin = this.decodeToken(token);
        Map<String, String> close = new HashMap<>();
        close.put("oId", oId);
        Operation order = this.operationService.getById(oId);

        Operation operation = new Operation();
        operation.setAdminId(admin.getAdminId());
        operation.setOrderId(oId);
        operation.setOperationTime(new Date());
        if ("---".equals(des)) {
            operation.setOperationDes("关闭订单");


        }else {
            operation.setOperationDes(des);
        }



        this.operationService.saveOrUpdate(operation);

        if (this.adminOrderService.updateClose(close) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 订单发货
     * @param logisticsCom  物流公司
     * @param logisticsId 物流单号
     * @param oId 订单号
     * @param token token
     * @return
     */
    @GetMapping("deliver")
    public R orderDeliver(String logisticsCom, String logisticsId, String oId, @RequestHeader(value = "access-token") String token) {
        Admin admin = this.decodeToken(token);
        Map<String, String> deliver = new HashMap<>();
        deliver.put("logisticsCom", logisticsCom);
        deliver.put("logisticsId", logisticsId);
        deliver.put("oId", oId);
        Operation operation = new Operation();
        operation.setAdminId(admin.getAdminId());
        operation.setOrderId(oId);
        operation.setOperationTime(new Date());
        operation.setOperationDes("发货");

        this.operationService.saveOrUpdate(operation);

        if (this.adminOrderService.updateDeliver(deliver)>0) {
            return  R.ok();
        }
        return R.error();

    }

    /**
     * 删除订单
     * @param oId 订单id
     * @param token token
     * @return
     */
    @GetMapping("del")
    public R delOrder(String oId, @RequestHeader(value = "access-token") String token){
        Admin admin = this.decodeToken(token);
        Operation operation = new Operation();
        operation.setAdminId(admin.getAdminId());
        operation.setOrderId(oId);
        operation.setOperationTime(new Date());
        operation.setOperationDes("删除订单");

        this.operationService.save(operation);

        if (this.adminOrderService.removeById(oId)) {
            return R.ok();
        }

        return R.error();
    }


    @GetMapping("/option")
    public R getOperation(String oId){

        QueryWrapper<Operation> operationQueryWrapper = new QueryWrapper<>();
        operationQueryWrapper.eq("order_id",oId);
        List<Operation> list = this.operationService.list(operationQueryWrapper);
        return R.ok(list);

    }




    private Admin decodeToken(String token) {

        String s = JWTUtil.analysisToken(token);
        Map map = JSON.parseObject(s, Map.class);
        Admin admin = this.adminService.getById(map.get("adminId").toString()); //1平台 0店铺

        return admin;
    }


}
